package client;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import server.ClientConnection;
import server.ServerProgram;
import dataContracts.CommandParser;
import dataContracts.CommandType;
import UI.UI;

public class MessageHandler {
	public ClientProgram program;
	public UI ui;
	
	MessageHandler(ClientProgram program) {
		this.program = program;
	}
	
	public void AddLogMsg(String msg) {
		ui.servLog(msg);
	}
	
	public void RequestSynch() {
		sendCommand(CommandType.Synch, program.getFileList());
	}
	
	public void RequestUpload(String file) {
		sendCommand(CommandType.RecvFile, file);
	}
	
	public void GetFile(String fileName) {
		sendCommand(CommandType.GetFile, fileName);
	}

	public class Broadcast implements ActionListener	{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			sendCommand(CommandType.Broadcast, ui.broadcastText.getText());
			ui.broadcastText.setText("");
		}
	}
	
	public class IsEqual implements ActionListener	{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			program.directory.refresh();
			sendCommand(CommandType.IsEqual, program.directory.getChecksum());
		}
	}
	
	private void sendCommand(CommandType type, String command) {
		program.writer.println(CommandParser.buildCommand(type, command));
		program.writer.flush();
	}
}
